/*
* $Id: TrueColorToolBar.cpp 108 2010-06-29 16:54:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Alexey Volokitin
* All rights reserved
*
*/

#include "Common/include.h"
#include "TrueColorToolBar.h"

CTrueColorToolBar::CTrueColorToolBar(int nCount)
: m_nCount(nCount){
	m_ids = new UINT[m_nCount];
}
CTrueColorToolBar::~CTrueColorToolBar(){
	delete m_ids;
}

BEGIN_MESSAGE_MAP(CTrueColorToolBar, CToolBar)
	ON_WM_CREATE()
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()

BOOL CTrueColorToolBar::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
	TOOLTIPTEXT* pTTTW = (TOOLTIPTEXT*)pNMHDR;
	TCHAR szFullText[512] = {0};
	UINT nID = (UINT)pNMHDR->idFrom;
	if (nID != 0){
		memset(pTTTW->szText, 0, 80*sizeof(TCHAR));
		memcpy(pTTTW->szText, m_idsString[nID].c_str(), m_idsString[nID].size()* sizeof(TCHAR));
	}
	::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
		SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER);
	return TRUE;
}

BOOL CTrueColorToolBar::SetTrueColorToolBar(UINT uToolBarType, UINT uToolBar, int nBtnWidth)
{
	CImageList	cImageList;
	CBitmap		cBitmap;
	if (!cBitmap.Attach(LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uToolBar), IMAGE_BITMAP, 0, 0,
								  LR_DEFAULTSIZE|LR_CREATEDIBSECTION)))
		return FALSE;
	if (!cImageList.Create(nBtnWidth, nBtnWidth, ILC_COLOR32|ILC_MASK, 0, 0))
		return FALSE;
	if (cImageList.Add(&cBitmap, RGB(255,255,255)) == -1)
		return FALSE;
	SendMessage(uToolBarType, 0, (LPARAM)cImageList.m_hImageList);
	cImageList.Detach(); 
	cBitmap.Detach();
	return TRUE;
}

void CTrueColorToolBar::SetActiveButton(int nButton, BOOL bActive)
{
	UINT *ids = new UINT[m_nCount];
	memcpy(ids, m_ids, sizeof(UINT)*m_nCount);
	delete m_ids;
	m_ids = ids;
	if(!bActive)
		m_ids[nButton] = 1;
	SetButtons();
}

void CTrueColorToolBar::SetButtons(){
	__super::SetButtons(m_ids, m_nCount);
}

int CTrueColorToolBar::OnCreate(LPCREATESTRUCT lpcs){
	FillIds(m_ids, m_idsString);
	return __super::OnCreate(lpcs);
}